package com.meisupic.copyright.controller;

import com.meisupic.copyright.common.MonitorStatusEnum;
import com.meisupic.copyright.domain.bo.SysUser;
import com.meisupic.copyright.domain.po.CopyRightWorks;
import com.meisupic.copyright.domain.po.MonitorRecord;
import com.meisupic.copyright.domain.po.MonitorResult;
import com.meisupic.copyright.domain.query.MonitorQeury;
import com.meisupic.copyright.domain.vo.MonitorRecordVo;
import com.meisupic.copyright.domain.vo.MonitorResultVo;
import com.meisupic.copyright.service.ICopyRightWorksService;
import com.meisupic.copyright.service.IMonitorService;
import com.meisupic.copyright.support.Message;
import com.meisupic.copyright.util.BeanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author lanjian
 * @version V1.0
 * @Description: TODO
 * @date 2018/3/15 23:27
 */

@RestController
@RequestMapping("/copyright/monitor")
public class MonitorController extends BaseController {

  @Autowired
  private IMonitorService monitorService;

  @Autowired
  private ICopyRightWorksService copyRightWorksService;

  @GetMapping("/recordList")
  public Page<MonitorRecordVo> list(MonitorQeury monitorQeury,
      @PageableDefault(sort = {"createTime"},direction= Direction.DESC) Pageable pageable) {
    monitorQeury.setUserId(getUserId());
    return monitorService.getPageMonitorRecord(monitorQeury, pageable);
  }

  @PostMapping("/save")
  public Message save(MonitorRecordVo monitorRecordVo) {

    Long worksId = monitorRecordVo.getWorksId();

    CopyRightWorks copyRightWorks = copyRightWorksService.findById(worksId);
    monitorRecordVo.setWorkImgUrl(copyRightWorks.getImageName());
    monitorRecordVo.setWorksName(copyRightWorks.getWorksFullName());
    monitorRecordVo.setWorksType(copyRightWorks.getWorksType());

    SysUser user = getUser();
    monitorRecordVo.setUserId(user.getId());
    monitorRecordVo.setUserName(user.getUsername());
    monitorRecordVo.setStatus(MonitorStatusEnum.running);
    MonitorRecord monitorRecord = BeanMapper.map(monitorRecordVo, MonitorRecord.class);
    monitorRecordVo = monitorService.saveMonitorRecord(monitorRecord);
    return Message.success(monitorRecordVo);
  }

  @PostMapping("/changeStatus")
  public Message changeStatus(Long id) {
    MonitorRecordVo monitorRecordVo = monitorService.changeStatus(id);
    return Message.success(monitorRecordVo);
  }


  @GetMapping("/delete")
  public Message delete(Long id) {
    monitorService.deleteMonitorRecore(id);
    return Message.success(id);
  }

  @GetMapping("/resultList")
  public Page<MonitorResultVo> resultList(MonitorResult monitorResult,
      @PageableDefault(sort = {"createTime"},direction= Direction.DESC) Pageable pageable) {
    SysUser user = getUser();
    monitorResult.setUserId(user.getId());
    return monitorService.getPageMonitorResult(monitorResult, pageable);
  }

  @GetMapping("/ingore")
  public Message ingore(Long id) {
    monitorService.ingoreResult(id);
    return Message.success(id);
  }

}
